<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */


class b2c_mdl_member_company extends dbeav_model{

    /* member address number limit */
    public $consLimit = 6;

    public function set_default_coms($arr_data=array(), $company_id=0, $member_id=0, &$msg='')
    {
        $filter = array('member_id' => $member_id);
        $cnt = $this->count($filter);
        if ($cnt <= $this->consLimit && $arr_data['def_company']){
            $arr_update['def_company'] = 0;
            $is_updated = $this->update($arr_update, $filter);
        }
        if ($company_id){
            $is_updated = $this->update($arr_data, array('company_id' => $company_id));
        }else{
            if ($cnt <= $this->consLimit){
                $is_updated = $this->save($arr_data);
            }else{
				$msg = app::get('b2c')->_('最多只能添加6个付款公司，请先删除一条之后再添加');
                $is_updated = false;
			}
        }
        return $is_updated;
    }

    //获取订单确认页默认付款人 by yyj 2015/12/28
    public function get_default_company($member_id=null){
        $this->obj_session = kernel::single('base_session');
        $this->obj_session->start();
        $obj_member_com = $this->app->model('member_company');
        //缓存地址判断
        if($member_id && $_COOKIE['company']['usable'] && $_COOKIE['company']['usable'] == md5($this->obj_session->sess_id().$member_id)){
            $def_com_id = $_COOKIE['company']['consignee_id'];
            $tmp_def_coms = $obj_member_com->getList('*',array('company'=>$def_com_id));
            $tmp_def_coms ? $def_com = $tmp_def_com[0] : $def_com = null;
        }else{
            $def_com = null;
        }

        //是否有会员绑定收货人
        $tmp_cnt = $obj_member_com->getList('*',array('member_id'=>$member_id,'def_company'=>'1'));
        if(!$def_com && $tmp_cnt) $def_com = $tmp_cnt[0];

        if(!$def_com){
            $tmp_company = $obj_member_com->getList('*',array('member_id'=>$member_id));
            $def_com = $tmp_company[0];
        }
        return $def_com;
    }
}
